﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;

    public class AccessorialChargesDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected DropDownList ddlAccessorial;
        protected DropDownList ddlBillingObject;
        protected DropDownList ddlBillingUnit;
        protected DropDownList ddlCostType;
        protected DropDownList ddlDueFromTo;
        protected HtmlForm F9183;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwenrID;
        protected HtmlInputHidden hdnTariffID;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected HtmlImage imgOwnerID;
        protected Label lblAccessorial;
        protected Label lblAccessorialChargesID;
        protected Label lblAreaDetail;
        protected Label lblBillingAmount;
        protected Label lblBillingObject;
        protected Label lblBillingObjectID;
        protected Label lblBillingQty;
        protected Label lblBillingRate;
        protected Label lblBillingUnit;
        protected Label lblCostType;
        protected Label lblDueFromTo;
        protected Label lblLineID;
        protected Label lblLot;
        protected Label lblMsg;
        protected Label lblOrderID;
        protected Label lblQty;
        protected Label lblRemark;
        protected Label lblSkuID;
        protected Label lblTotalVolume;
        protected Label lblTotalWeight;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAccessorialChargesID;
        protected TextBox txtBillingAmount;
        protected TextBox txtBillingObject;
        protected TextBox txtBillingQty;
        protected TextBox txtBillingRate;
        protected TextBox txtLineID;
        protected TextBox txtLot;
        protected TextBox txtOrderID;
        protected TextBox txtQty;
        protected TextBox txtRemark;
        protected TextBox txtSkuID;
        protected TextBox txtTotalVolume;
        protected TextBox txtTotalWeight;
        protected UpdatePanel up1;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.hdnType.Value == "InvCharges")
            {
                base.Server.Transfer(string.Format("AccessorialChargesQuery.aspx", new object[0]));
            }
            else
            {
                base.Server.Transfer(string.Format("AccessorialCharges.aspx?from={0}&detail={1}&ID={2}&Type={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtOrderID.Text), base.UrlEncode(this.hdnType.Value) }));
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtBillingObject.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9180_010");
            }
            else
            {
                decimal tempDec = 0M;
                try
                {
                    AccessorialChargesInfo AccessorialChargesInfo = new AccessorialChargesInfo {
                        AccessorialChargesID = this.txtAccessorialChargesID.Text,
                        AccessorialType = this.hdnType.Value,
                        OwnerID = this.hdnOwenrID.Value,
                        BilledUnits = 0M,
                        Debit = 0M,
                        ChargePerUnit = 0M,
                        Lot = this.txtLot.Text,
                        OrderID = this.txtOrderID.Text
                    };
                    if (!string.IsNullOrEmpty(this.txtLineID.Text))
                    {
                        AccessorialChargesInfo.OrderLineID = int.Parse(this.txtLineID.Text);
                    }
                    else
                    {
                        AccessorialChargesInfo.OrderLineID = 0;
                    }
                    AccessorialChargesInfo.RateID = this.txtBillingRate.Text.Trim();
                    if (decimal.TryParse(this.txtBillingRate.Text.Trim(), out tempDec))
                    {
                        AccessorialChargesInfo.RateID = this.txtBillingRate.Text.Trim();
                    }
                    else
                    {
                        AccessorialChargesInfo.RateID = tempDec.ToString();
                    }
                    AccessorialChargesInfo.SkuID = this.txtSkuID.Text;
                    if (!string.IsNullOrEmpty(this.txtQty.Text.Trim()))
                    {
                        AccessorialChargesInfo.Qty = decimal.Parse(this.txtQty.Text);
                    }
                    else
                    {
                        AccessorialChargesInfo.Qty = 0M;
                    }
                    AccessorialChargesInfo.IsChecked = "N";
                    AccessorialChargesInfo.AccessorialID = this.ddlAccessorial.SelectedValue;
                    AccessorialChargesInfo.CostType = this.ddlCostType.SelectedValue;
                    AccessorialChargesInfo.BillingObject = this.ddlBillingObject.SelectedValue;
                    AccessorialChargesInfo.BillingObjectID = this.txtBillingObject.Text.Trim();
                    AccessorialChargesInfo.DueFromTo = this.ddlDueFromTo.SelectedValue;
                    AccessorialChargesInfo.ChargeUnit = this.ddlBillingUnit.SelectedValue;
                    if (!string.IsNullOrEmpty(this.txtBillingQty.Text.Trim()))
                    {
                        if (decimal.TryParse(this.txtBillingQty.Text.Trim(), out tempDec))
                        {
                            AccessorialChargesInfo.BillingQty = decimal.Parse(this.txtBillingQty.Text);
                        }
                        else
                        {
                            AccessorialChargesInfo.BillingQty = tempDec;
                        }
                    }
                    else
                    {
                        AccessorialChargesInfo.BillingQty = 0M;
                    }
                    if (!string.IsNullOrEmpty(this.txtBillingAmount.Text.Trim()))
                    {
                        if (decimal.TryParse(this.txtBillingAmount.Text.Trim(), out tempDec))
                        {
                            AccessorialChargesInfo.BillingAmount = decimal.Parse(this.txtBillingAmount.Text);
                        }
                        else
                        {
                            AccessorialChargesInfo.BillingAmount = tempDec;
                        }
                    }
                    else
                    {
                        AccessorialChargesInfo.BillingAmount = 0M;
                    }
                    if (!string.IsNullOrEmpty(this.txtTotalWeight.Text.Trim()))
                    {
                        if (decimal.TryParse(this.txtTotalWeight.Text.Trim(), out tempDec))
                        {
                            AccessorialChargesInfo.TotalNetWeight = decimal.Parse(this.txtTotalWeight.Text);
                        }
                        else
                        {
                            AccessorialChargesInfo.TotalNetWeight = tempDec;
                        }
                    }
                    if (!string.IsNullOrEmpty(this.txtTotalVolume.Text.Trim()))
                    {
                        if (decimal.TryParse(this.txtTotalVolume.Text.Trim(), out tempDec))
                        {
                            AccessorialChargesInfo.TotalNetVolume = decimal.Parse(this.txtTotalVolume.Text.Trim());
                        }
                        else
                        {
                            AccessorialChargesInfo.TotalNetVolume = tempDec;
                        }
                    }
                    AccessorialChargesInfo.Remark = this.txtRemark.Text.Trim();
                    AccessorialChargesInfo.ChargeType = this.hdnType.Value;
                    AccessorialChargesInfo.CreatedBy = base.UserInfo.UserID;
                    this.lblMsg.Text = BillingServiceClientProxy.GetInstance().InsertAccessorialCharges(AccessorialChargesInfo) + base.GetGlobalMsgResource("F9180_008");
                    this.hdnFlag.Value = "1";
                    if (this.hdnType.Value != "InvCharges")
                    {
                        base.Server.Transfer(string.Format("AccessorialCharges.aspx?from={0}&detail={1}&ID={2}&Type={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtOrderID.Text), base.UrlEncode(this.hdnType.Value) }));
                    }
                    else
                    {
                        this.ddlBillingObject.SelectedValue = "Owner";
                        this.txtBillingObject.Text = "";
                        this.txtBillingRate.Text = "";
                        this.txtBillingQty.Text = "";
                        this.txtBillingAmount.Text = "";
                        this.txtTotalVolume.Text = "";
                        this.txtTotalWeight.Text = "";
                        this.txtRemark.Text = "";
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9180_009") + ex.Message;
                }
            }
        }

        protected void ddlAccessorial_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtBillingRate.Text = string.Empty;
            if (!string.IsNullOrEmpty(this.ddlAccessorial.SelectedValue))
            {
                DataSet dsAccessorial = null;
                if (base.Cache["Accessorial"] != null)
                {
                    dsAccessorial = (DataSet) base.Cache["Accessorial"];
                    DataRow[] dr = dsAccessorial.Tables[0].Select("ACCESSORIAL_ID = '" + this.ddlAccessorial.SelectedValue + "'");
                    if (dr.Length > 0)
                    {
                        decimal rate = decimal.Parse(dr[0]["RATE"].ToString());
                        if (rate > 0M)
                        {
                            this.txtBillingRate.Text = base.GetPreferenceDigit(rate);
                        }
                    }
                }
            }
        }

        protected void ddlBillingObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlBillingObject.SelectedValue.ToUpper() == "OWNER")
            {
                this.txtBillingObject.Text = this.hdnOwenrID.Value;
                this.txtBillingObject.Enabled = false;
            }
            else
            {
                this.txtBillingObject.Text = "";
                this.txtBillingObject.Enabled = true;
            }
        }

        protected void ddlBillingUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtBillingQty_TextChanged(null, null);
            this.txtTotalWeight_TextChanged(null, null);
            this.txtTotalVolume_TextChanged(null, null);
        }

        protected void ddlCostType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.ddlCostType.SelectedValue))
            {
                DataSet dsAccessorial = null;
                if (base.Cache["Accessorial"] != null)
                {
                    dsAccessorial = (DataSet) base.Cache["Accessorial"];
                }
                else
                {
                    dsAccessorial = BaseInfoServiceClientProxy.GetInstance().GetAllInfo("ACCESSORIAL");
                    base.Cache["Accessorial"] = dsAccessorial;
                }
                DataRow[] dr = dsAccessorial.Tables[0].Select("COST_TYPE = '" + this.ddlCostType.SelectedValue + "'");
                DataTable dt = dsAccessorial.Tables[0].Clone();
                for (int i = 0; i < dr.Length; i++)
                {
                    dt.ImportRow(dr[i]);
                }
                this.ddlAccessorial.DataSource = dt;
                this.ddlAccessorial.DataTextField = "DESCR";
                this.ddlAccessorial.DataValueField = "ACCESSORIAL_ID";
                this.ddlAccessorial.DataBind();
                this.ddlAccessorial.Items.Insert(0, "");
            }
        }

        private void GetBillingAmount()
        {
            if (!string.IsNullOrEmpty(this.txtBillingRate.Text.Trim()) && !string.IsNullOrEmpty(this.txtBillingQty.Text.Trim()))
            {
                decimal tempDec = 0M;
                decimal billingRate = 0M;
                decimal billingQty = 0M;
                if (decimal.TryParse(this.txtBillingRate.Text.Trim(), out tempDec))
                {
                    billingRate = decimal.Parse(this.txtBillingRate.Text.Trim());
                }
                else
                {
                    billingRate = tempDec;
                }
                if (decimal.TryParse(this.txtBillingQty.Text.Trim(), out tempDec))
                {
                    billingQty = decimal.Parse(this.txtBillingQty.Text.Trim());
                }
                else
                {
                    billingQty = tempDec;
                }
                this.txtBillingAmount.Text = (billingRate * billingQty).ToString();
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                string from = base.Request.QueryString["from"];
                this.hdnFrom.Value = from;
            }
            base.BindData2DDLBySysCode(this.ddlBillingUnit, "ChargeUnit");
            base.BindData2DDLBySysCode(this.ddlCostType, "CostType");
            base.BindData2DDLBySysCode(this.ddlBillingObject, "BillingObject");
            base.BindData2DDLBySysCode(this.ddlDueFromTo, "DueFromTo");
            this.ddlCostType_SelectedIndexChanged(null, null);
            this.txtOrderID.Text = base.Request.QueryString["OrderID"];
            this.txtLineID.Text = base.Request.QueryString["LineID"];
            this.txtSkuID.Text = base.Request.QueryString["SkuID"];
            this.txtLot.Text = base.Request.QueryString["Lot"];
            this.hdnType.Value = base.Request.QueryString["Type"];
            this.txtQty.Text = base.GetPreferenceDigit(base.Request.QueryString["Qty"]);
            this.hdnOwenrID.Value = base.Request.QueryString["OwnerID"];
            if ((this.ddlBillingObject.SelectedValue == "Owner") && (this.hdnType.Value != "InvCharges"))
            {
                this.txtBillingObject.Text = base.Request.QueryString["OwnerID"];
                this.txtBillingObject.Enabled = false;
            }
            else
            {
                this.txtBillingObject.Text = "";
                this.txtBillingObject.Enabled = true;
            }
            if (this.hdnType.Value == "InvCharges")
            {
                this.txtOrderID.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["Key"]))
            {
                this.txtLineID.Text = "0";
                this.txtLot.Text = base.GetGlobalMsgResource("F9180_004");
                this.txtQty.Text = "0";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        protected void txtBillingQty_TextChanged(object sender, EventArgs e)
        {
            if (this.ddlBillingUnit.SelectedValue.ToUpper() == "WEIGHT")
            {
                this.txtTotalWeight.Text = this.txtBillingQty.Text.Trim();
                this.txtTotalVolume.Text = "";
            }
            else if (this.ddlBillingUnit.SelectedValue.ToUpper() == "MEASUREMENT")
            {
                this.txtTotalVolume.Text = this.txtBillingQty.Text.Trim();
                this.txtTotalWeight.Text = "";
            }
            this.GetBillingAmount();
        }

        protected void txtBillingRate_TextChanged(object sender, EventArgs e)
        {
            this.GetBillingAmount();
        }

        protected void txtTotalVolume_TextChanged(object sender, EventArgs e)
        {
            if (this.ddlBillingUnit.SelectedValue.ToUpper() == "MEASUREMENT")
            {
                this.txtBillingQty.Text = this.txtTotalVolume.Text.Trim();
            }
            else if (this.ddlBillingUnit.SelectedValue.ToUpper() == "BILLINGTON")
            {
                if (string.IsNullOrEmpty(this.txtTotalWeight.Text.Trim()))
                {
                    this.txtBillingQty.Text = this.txtTotalVolume.Text.Trim();
                }
                else if (!string.IsNullOrEmpty(this.txtTotalVolume.Text.Trim()))
                {
                    if (decimal.Parse(this.txtTotalWeight.Text.Trim()) > decimal.Parse(this.txtTotalVolume.Text.Trim()))
                    {
                        this.txtBillingQty.Text = this.txtTotalWeight.Text.Trim();
                    }
                    else
                    {
                        this.txtBillingQty.Text = this.txtTotalVolume.Text.Trim();
                    }
                }
            }
            this.GetBillingAmount();
        }

        protected void txtTotalWeight_TextChanged(object sender, EventArgs e)
        {
            if (this.ddlBillingUnit.SelectedValue.ToUpper() == "WEIGHT")
            {
                this.txtBillingQty.Text = this.txtTotalWeight.Text.Trim();
            }
            else if (this.ddlBillingUnit.SelectedValue.ToUpper() == "BILLINGTON")
            {
                if (string.IsNullOrEmpty(this.txtTotalVolume.Text.Trim()))
                {
                    this.txtBillingQty.Text = this.txtTotalWeight.Text.Trim();
                }
                else if (!string.IsNullOrEmpty(this.txtTotalWeight.Text.Trim()))
                {
                    if (decimal.Parse(this.txtTotalVolume.Text.Trim()) > decimal.Parse(this.txtTotalWeight.Text.Trim()))
                    {
                        this.txtBillingQty.Text = this.txtTotalVolume.Text.Trim();
                    }
                    else
                    {
                        this.txtBillingQty.Text = this.txtTotalWeight.Text.Trim();
                    }
                }
            }
            this.GetBillingAmount();
        }
    }
}

